/**
 * author:zhangjlt
 * date:2016年9月2日3:18:42
 */
(function(){
	var industryAdd = {
		//事件绑定
		bindEvent :function(){
			var index = parent.layer.getFrameIndex(window.name); 
			//获取窗口索引	
	    	window.sessionStorage.layerIndex = index;
			// 确定按钮
			$("#saveSettingLayer").unbind().on("click", function () {
				
				industryAdd.saveSettingClick();
				
			});
			// 取消按钮
			$("#cancleButtonLayer").unbind().on("click", function () {
				parent.layer.close(index);
			});
			$("#productchoose-btnLayer").unbind().on("click", function () {
				
					// 页面一打开就执行，放入ready是为了layer所需配件（css、扩展模块）加载完毕 
					parent.layer.ready(function(){ 
			  //增加编辑
					   parent.layer.open({
					    type: 2,
					    //skin: 'layui-layer-lan',
					    title: '增加/编辑',
					    fix: false,
					    shadeClose: true,
					    maxmin: true,
					    area: ['1000px', '500px'],
					    content: 'html/keyIndustryMaintanceChoose.html',
					  });
					});
					});
				
				
		},	
		saveSettingClick:function(){
			if (!$("#productNameInputLayer").val() ) {
					var name = $("#productNameLabelLayer").text();
					name = name.substring(0,name.length-1);
					parent.layer.msg("请先输入"+name);
			}else if(!$("#orderNumInputLayer").val()){
				parent.layer.msg("请先输入显示序号");
			}else{
						
			   pId = $("#productNameInputLayer").text();
			   id = $("#orderNumInputLayer").text();	
			   var fw_parent= pId;
           	   var param = {
				  "fw_parent":fw_parent,
				  "cate_name":$("#productNameInputLayer").val(),
				  "order_num": $("#orderNumInputLayer").val()
				}
				//调用ajax
				var ajaxUrl = "/PCIndustryChart/insert";
				//转圈提示
    			common.showLoading();
				common.interfaceAjaxFunction(ajaxUrl,param,industryAdd.insertSucess);
				
				}
		},
		insertSucess:function(res){
			
			common.hideLoading();
			if (res.status==0) {
				var zTree = parent.$.fn.zTree.getZTreeObj("leftTreeIndustry");
				nodes = zTree.getSelectedNodes();
				treeNode = nodes[0];
				zTree.addNodes(treeNode, {id:res.data.pk_industry_chart, pId:res.data.fw_parent, name:res.data.cate_name,order_num:res.data.order_num,});
                
                var str = window.sessionStorage.newtreeNode;
			    var treeNode = JSON.parse(str);
                if(treeNode.level==3 &&$("#kmProductInputLayer").val().length>0){
                	 var str = window.sessionStorage.key_productId;
				       var productIds = new Array;
				       if(str){
				       	productIds = str.split(",");
				       }
					   	var param = {
					   		"pk_user":window.localStorage.userid,
					   		"pk_industry_chart":res.data.pk_industry_chart,
					   		"pk_product_list":productIds
					   		}
							//调用ajax
						var ajaxUrl = "/pc/industryProduct/insert";
						//转圈提示
		    			common.showLoading();
						common.interfaceAjaxFunction(ajaxUrl,param,industryAdd.productInsertSucess);
                }else{
                	parent.layer.msg("新增成功");
                	var index = parent.layer.getFrameIndex(window.name); 
				    parent.layer.close(index);
                }
				
                
			}else{
				parent.layer.msg(res.message);
			}
			
		},
		productInsertSucess:function(res){
			
			if(res.status==0){
				var index = parent.layer.getFrameIndex(window.name); 
				parent.layer.msg(res.message);
				parent.layer.close(index);
			}else{
				parent.layer.msg(res.message);
			}
			
		},
		initData:function(){
			var str = window.sessionStorage.newtreeNode;
			var treeNode = JSON.parse(str);
			industryAdd.showTreeNode(treeNode);
		},
		showTreeNode:function(treeNode){
			if (!treeNode.productNameLabel || treeNode.productNameLabel.length ==0) {
				var productNameLabel = "商品名称：";
				switch (treeNode.level) {
				   	case 0:
				   		productNameLabel = "一级商品名称：";
						break;
					case 1:
						productNameLabel= "二级商品名称：";
						break;
					case 2:
						productNameLabel= "三级商品名称：";
						break;
					case 3:
						productNameLabel= "商品名称：";
						break;
					case 4:
						productNameLabel= "赠品名称：";
						break;
					case 5:
						productNameLabel = "规格名称：";
						break;
					default:
						productNameLabel = "归属地名称：";
						break;
			   }
			   treeNode.productNameLabel=productNameLabel;
			}
			if(treeNode.level == 3){
			   		$("#kmProduct").show();
			   }else{
			   		$("#kmProduct").hide();
			   }
			   $("#productNameLabelLayer").text(treeNode.productNameLabel);
			   // 名字 cate_name
			   $("#productNameInputLayer").val(treeNode.name);
			   // 父节点id fw_parent
			   $("#productNameInputLayer").text(treeNode.pId);
			   // 排序 order_num
			   $("#orderNumInputLayer").val(treeNode.order_num);
			   //  节点id pk_industry_chart
			   $("#orderNumInputLayer").text(treeNode.id);
			   
		}
		
	};
	$(document).ready(function(){
		industryAdd.bindEvent();
		industryAdd.initData();			
	});
})();